<?
/**
 * request.php
 *
 * <pre>
 * Devyra Internacionalnaya
 * Vebba PHP Framework and Website Builder
 * Source file for HTTP request data handling
 * </pre>
 *
 * @package   classes.core.static
 * @version   0.7.0102 alpha
 * @since     2012
 * @author    Paul Dinh
 * @copyright Devyra Internacionalnaya
 */

/**
 * HTTP request data handling class
 * 
 * @package classes.core.static
 * @version 0.7.0102 alpha
 * @since   2012
 * @author  Paul Dinh
 */
class request {
  
  /*
   * All properties
   */
  public static $Module  = "";
  public static $Page    = "";
  
  //list of actions by names
  public static $Actions = array(); 
  
  /*
   * e.g.
   * For action search:
   * $Params["search"] is an associative array
   * $Params["search"]["title"] is the value to search in title
   * $Params["search"]["content"] is the value to search in content, etc.
   *
   * URL example:
   * http://example.com/frontend/articles/search/title-abc/content-xyz
   */
  public static $Params = array();  

  /**
   * Standard initialisation
   */
  public static function initialise() {
    //
  }
  
  /**
   * Check if there is a certain action in request
   *
   * @param string $Action_Name The action name to check
   */
  public static function action_exists($Action_Name) {
    return in_array($Action_Name,self::$Actions);
  }
  
  /**
   * Find default page name for a module as sated in config.xml   
   */
  private static function get_def_page($Module_Name) {
    $Configs       = system::$Configs;
    $Default_Pages = $Configs["default"]["pages"]["page"];
    $Default_Page  = null;
    
    //scan thru' the list of default pages
    foreach ($Default_Pages as $Page)
      if ($Page["@attributes"]["for"]==$Module_Name) {
        $Default_Page = $Page["@attributes"]["name"];
        break;
      }      
    return $Default_Page;
  }
  
  /**
   * Get POST data
   *
   * @return array The array of POST data ($_POST)
   */
  public static function get_post() {
    return $_POST;
  }
  
  /**
   * Parse query string into module name, page name, and params
   */
  public static function parse_query_str() {
    $Query_String = trim($_SERVER["QUERY_STRING"]);
    $Query_String = preg_replace("/[\/\\\\]+$/","",$Query_String);
    $Query_String = preg_replace("/^[\/\\\\]+/","",$Query_String);
    
    //get default module name and its default page name
    $Configs        = system::$Configs;
    $Default_Module = $Configs["default"]["module"];
    $Default_Page   = self::get_def_page($Default_Module);    
      
    //check if default page for default module is found
    if ($Default_Page==null)
      show_err(phrase::__(
      "CAN_NOT_FIND_DEFAULT_PAGE_NAME_IN_CONFIG_FILE_FOR_MODULE").
      "\x20'".$Default_Module."'");
      
    //check if module name stated in query string
    if (strlen($Query_String)==0) {
      self::$Module  = $Default_Module;
      self::$Page    = $Default_Page;
      self::$Actions = array("index");
      self::$Params  = array("index"=>array());
      return;
    }
    
    //$Tokens[0] is supposed to be the module name,
    //in case module name is not in query string
    //the default module is used.
    $Tokens  = explode("/",$Query_String);
    $Modules = trim(system::$Configs["modules"]);
    $Modules = str_replace(chr(32),"",$Modules);
    $Modules = explode(",",$Modules); //string to array
    if (!in_array($Tokens[0],$Modules))
      $Query_String = $Default_Module."/".$Query_String;
    
    //check if only module name is in query string
    $Tokens = explode("/",$Query_String);
    if (count($Tokens)==1) {
      self::$Module  = $Query_String;
      self::$Page    = self::get_def_page(self::$Module);
      self::$Actions = array("index");
      self::$Params  = array("index"=>array());
      return;
    }
    
    //check if only module name and page name is in query string
    if (count($Tokens)==2) {
      self::$Module  = $Tokens[0];
      self::$Page    = $Tokens[1];
      self::$Actions = array("index");
      self::$Params  = array("index"=>array());
      return;
    }
    
    //in case there are more than 2 tokens
    self::$Module  = $Tokens[0];
    self::$Page    = $Tokens[1];
    self::$Actions = array("index");
    self::$Params  = array("index"=>array());
    
    $Current_Action = "index";
    for ($Index=2; $Index<count($Tokens); $Index++)
      if (strpos($Tokens[$Index],"-")===false) {
        $Token = $Tokens[$Index];
        if (!in_array($Token,self::$Actions))
          array_push(self::$Actions,$Token);
        
        //set current action
        $Current_Action = $Token;
        self::$Params[$Token] = array();
      }
      else {
        $Token = $Tokens[$Index];
        $Dash_Pos = strpos($Token,"-");
        if ($Dash_Pos===false || $Dash_Pos===0)
          continue;
          
        //split param name & param value
        $Name  = substr($Token,0,$Dash_Pos);
        $Value = substr($Token,$Dash_Pos+1);
        self::$Params[$Current_Action][$Name] = $Value;
      }
  }//parse_query_str
}

//END-OF-FILE